package com.db.ctl;

import com.db.entity.Book;
import com.db.entity.BookJpa;
import com.db.query.BookQuery;
import com.db.query.QObject;
import com.db.repository.BookMapper;
import com.db.utils.QueryMybatisPlusUtils;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dzh
 * @since 2024/12/31
 */
@RestController
@RequestMapping("book")
@RequiredArgsConstructor
public class BookController {
    private final BookMapper bookMapper;
    private final JPAQueryFactory jpaQueryFactory;

    @PostMapping("page")
    public Object page(@RequestBody BookQuery bookQuery) {
        QObject<BookJpa> qObject = new QObject<>(BookJpa.class, bookQuery);
        return jpaQueryFactory.select(qObject)
                .from(qObject)
                .where(qObject.predicate())
                .fetch();
    }

    @PostMapping("pageMybatis")
    public Object pageMybatis(@RequestBody BookQuery bookQuery) {
        return bookMapper.selectList(QueryMybatisPlusUtils.queryWrapper(bookQuery, Book.class));
    }
}
